package com.atguigu.flink.chapter07.timer;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/17 14:53
 */
public class Flink03_Project_Timer {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String line) throws Exception {
                    String[] data = line.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner((ws, ts) -> ws.getTs())
            )
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                boolean isFirst = true;
                long time;
                int lastVc;
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    /*
                    1. 当第一条数据来了, 先注册一个5s后触发的定时器
                    2. 后面的数据过来, 如果水位相比上个水位是上升, 则不需任何操作
                    3. 如果后面的数据过来,相比上条数据是下降或者不变, 则删除定时器, 重新以当前数据的事件时间为准
                        注册一个新的定时器
                     */
                    if (isFirst) {
                        isFirst = false;
                        time = value.getTs() + 5000;
                        System.out.println("第一条数过来, 注册定时器:" + time);
                        ctx.timerService().registerEventTimeTimer(time);
                    } else { // 不是第一条数据
                        if (value.getVc() > lastVc) {
                            System.out.println("水位上升, 无需操作...");
                        } else {
                            System.out.println("水位没有上升, 注销上次注册的定时器");
                            ctx.timerService().deleteEventTimeTimer(time);
                            time = value.getTs() + 5000;
                            System.out.println("再注册新的定时器:" + time);
                            ctx.timerService().registerEventTimeTimer(time);
                        }
                    }
                    
                    // 处理完所有逻辑之后,把当前的水位保存到上一个水位, 等下一次使用
                    lastVc = value.getVc();
                    
                }
                
                @Override
                public void onTimer(long timestamp,
                                    OnTimerContext ctx,
                                    Collector<String> out) throws Exception {
                    out.collect("连续5s上升, 发出预警...");
                    isFirst = true;
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
